/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.action;

import com.rw.magallanes.core.Category;
import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.ide.main.MainFrame;
import com.rw.magallanes.ide.main.MainSystem;
import com.rw.magallanes.utilities.MessageType;
import com.rw.magallanes.utilities.Utilities;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

/**
 *
 * @author Gonzalez
 */
public class RenameCategoryAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
        MainSystem ms = MainSystem.getInstance();
        MainFrame mf = ms.getMainFrame();
        mf.stopEditingItemsTable();

        Category selectedCategory = mf.getSelectedCategory();
        if (selectedCategory != null) {

            String input = (String) JOptionPane.showInputDialog(mf, "Nuevo Nombre", "Renombrar", JOptionPane.INFORMATION_MESSAGE, null, null, selectedCategory.getName());
            if (input != null) {
                if (!input.isEmpty()) {

                    if (input.compareTo(selectedCategory.getName()) == 0) {
                        return;
                    }

                    Warehouse wh = mf.getWarehouse();
                    if (!wh.existsCategoryName(input)) {
                        selectedCategory.setName(input);
                        mf.updateCategoriesTable();
                    } else {
                        Utilities.showMsg(mf, "La categoria ya existe", MessageType.WARN);
                    }
                } else {
                    Utilities.showMsg(mf, "Nombre invalido", MessageType.WARN);
                }
            }
        } else {
            Utilities.showMsg(mf, "Seleccione una categoria", MessageType.WARN);
        }
    }

    @Override
    public Object getValue(String key) {
        if (key.compareTo(NAME) == 0) {
            return "Renombrar";
        } else {
            return super.getValue(key);
        }
    }
}
